//########################################################################
//## (C) by Edwin Vriethoff under Microsoft Reciprocal License (Ms-RL)  ##
//## See http://www.codeplex.com/SPUserPoll/license for full license    ##
//##                                                                    ##
//## http://edwin.vriethoff.net http://www.codeplex.com/SPUserPoll/     ##
//########################################################################

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.SharePoint;

namespace SPUserPoll.Data.Components
{
    public class ResultTable
    {
        private string _pollID = null;

        /// <summary>
        /// The pollID
        /// </summary>
        public string PollID
        {
            get { return _pollID; }
            set { _pollID = value; }
        }

        public ResultTable(string pollID)
        {
            PollID = pollID;
        }

        /// <summary>
        /// Returns a datatable with the poll results
        /// </summary>
        /// <param name="pollID"></param>
        /// <returns></returns>
        public DataTable ReturnResultTable()
        {

            //construct table header
            DataTable returnTable = new DataTable("ResultTable");
            returnTable.Columns.Add("Answer", Type.GetType("System.String"));
            returnTable.Columns.Add("AnswerID", Type.GetType("System.Int32"));
            returnTable.Columns.Add("Count", Type.GetType("System.Int32"));
            returnTable.Columns.Add("Percentage", Type.GetType("System.Decimal"));

            //get poll ansers
            List<SPListItem> pollAnswers = GetAvailablePollAnswers();

            //get total number of responses given
            int responseCount = GetTotalPollResponses();

            ResponseList responseList = new ResponseList();

            //calculate data for each item and add it to the table
            foreach (SPListItem item in pollAnswers)
            {
                string answer = item.Title.ToString();
                int answerID = item.ID;
                int count = responseList.GetCountForResponse(answer, PollID);
                decimal percentage = 0;
                if (responseCount!=0)
                    percentage = (Convert.ToDecimal(count) / Convert.ToDecimal(responseCount)) * 100;

                DataRow newRow = returnTable.NewRow();
                newRow["Answer"] = answer;
                newRow["AnswerID"] = answerID;
                newRow["Count"] = count;
                newRow["Percentage"] = percentage;

                returnTable.Rows.Add(newRow);
            }

            //return the table
            return returnTable;
        }

        /// <summary>
        /// returns the total number of available responses
        /// </summary>
        /// <returns></returns>
        private int GetTotalPollResponses()
        {
            ResponseList responseList = new ResponseList();
            return responseList.GetTotalResponseCount(PollID);
        }

        /// <summary>
        /// Returns all available poll answers
        /// </summary>
        /// <param name="pollID"></param>
        /// <returns></returns>
        private List<SPListItem> GetAvailablePollAnswers()
        {
            List<SPListItem> returnList = new List<SPListItem>();
            AnswersList answerList = new AnswersList();
            foreach (SPListItem item in answerList.GetPollAnswerListItems(PollID))
            {
                returnList.Add(item);
            }
            return returnList;
        }
    }
}
